package com.dullwolf.utils.card;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class CardTask {

    public static int count = 1;
    public static String lastCardStr = "-1";
    public static int lastHost = -1;
    public static int winHost = -1;

    //三个玩家的线程

    //三个玩家的牌面
    private ArrayList<String> p1Cards = new ArrayList<>();
    private ArrayList<String> p2Cards = new ArrayList<>();
    private ArrayList<String> p3Cards = new ArrayList<>();


    private void sendCardMsg(int count) {


        FileInputStream fis = null;
        File file = null;
        try {
            file = new File("玩家" + count + ".txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != file) {
            System.out.println("请玩家 " + count + " 输入牌面信息：");
            while (true) {
                try {
                    if (file.exists()) {
                        boolean delete = file.delete();
                        //System.out.println("删除成功？" + delete);
                    }
                    file.createNewFile();
                    Thread.sleep(500);

                    fis = new FileInputStream(file);
                    System.setIn(fis);
                    Scanner sc = new Scanner(System.in);

                    boolean tag = false;
                    if (lastHost == count) {
                        //自己获得主动权，重新发牌
                        System.out.println("玩家" + count + "获得主动权");
                        tag = true;

                    }

                    String cardInfo = sc.nextLine();
                    //System.out.println(cardInfo);
                    String info = cardInfo.toUpperCase();
                    if (info.equalsIgnoreCase("pass")) {
                        System.out.println("玩家" + count + "已跳过");
                        break;
                    }

                    ArrayList<String> list = getCardList(count);
                    String cardType = RuleUtil.checkCard(info, list);
                    if (!RuleUtil.ERROR.equals(cardType)) {
                        if (!"-1".equals(lastCardStr)) {
                            //比较之前需要看双方牌数目是否一致(除了炸弹之外)
                            if (lastCardStr.length() != info.length() && !RuleUtil.Bomb.equals(cardType) && !tag) {
                                System.out.println("牌面数目有误");
                            } else {
                                //校验牌面的大小规则
                                if (RuleUtil.compare(info, lastCardStr) || tag) {
                                    RuleUtil.removeCard(info, list);
                                    lastCardStr = info;
                                    break;
                                } else {
                                    System.out.println("你的牌小于上一个玩家的牌");
                                }
                            }

                        } else {
                            RuleUtil.removeCard(info, list);
                            lastCardStr = info;
                            break;
                        }

                    } else {
                        System.out.println("牌面有误");
                    }


                } catch (Exception ignored) {

                } finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }

    }


    private ArrayList<String> getCardList(int count) {
        ArrayList<String> list;
        if (count == 1) {
            list = p1Cards;
        } else if (count == 2) {
            list = p2Cards;
        } else {
            list = p3Cards;
        }
        return list;
    }

}
